$(document)
		.ready(
				function() {
					$("#set-option").unbind("click");
					
					displayLang = $("#display-lang").val();
					Etype = $("#etype").val();
					Eid = $("#eid").val();
					Genre = $("#genre").val();
					Gid = $("#gid").val();
					uGid = $("#ugid").val();
					uGname = $("#ugname").val();
					initCount = 4;
//					alert(Gid+"|"+uGid);
					/* left navigation of my group datamember list */

					$(".create_button").click(
							function() {
								var name;
								if (displayLang == "en") {
									name = "untitled";
								} else if (displayLang == "zh") {
									name = "未命名";
								}
								var ds_type = "custom";
								var datasetId = "-1";

								$(".myentity .nav-select").parent().find(
										".nav-arrow").remove();
								$(".myentity .nav-select").removeClass(
										"nav-select");
								var gname = uGname;
								createEditDatasetBox(name, ds_type, datasetId, gname,
										Etype, Eid);
							});

					$(".leftnav a").live("click", function() {
						if (!$(this).hasClass("nav-select")) {
							$(".leftnav a.nav-select").each(function() {
								$(this).removeClass("nav-select");
								$(this).parent().find(".nav-arrow").remove();
							});
							$(this).addClass("nav-select");

							var span = $("<span class='nav-arrow'></span>");
							$(this).parent().append(span);
						}

						var name = $(this).html();
						var etype = $(this).attr("etype");
						var eid = $(this).attr("eid");
						var gid = $(this).attr("gid");
						var gname = $(this).attr("gname");
						createEditDatasetBox(name, etype, eid, gname);
					});

					$(".leftnav a").live("mouseover", function() {
						$(this).addClass("bg-select");
					});

					$(".leftnav a").live("mouseout", function() {
						$(this).removeClass("bg-select");
					});
					/* end:left navigation of my group datamember list */

					/* init :show the exist entity in the left nav */
					init();

					$("a.close_button").live(
							"click",
							function() {
								var box = $(this).parent().parent().parent()
										.parent();
								var etype = box.attr('etype');
								var eid = box.attr("eid");
								unselect($(
										".leftnav a[etype=" + etype + "][eid="
												+ eid + "]").parent());
								box.remove();
							});

					$(".add-uri-op").live("mouseover", function() {
						$(this).addClass("hover");
					});

					$(".add-uri-op").live("mouseout", function() {
						$(this).removeClass("hover");
					});

					$(".rentity").live("mouseover", function() {
						$(this).find(".add-uri-op").show();
					});

					$(".rentity").live("mouseout", function() {
						$(this).find(".add-uri-op").hide();
					});
					/* end:init :show the exist entity in the left nav */

					/* uri memember operation */
					$(".content .item").live("mouseover", function() {
						$(this).addClass("bh");
						$(this).find(".opBox").show();

					});

					$(".content .item").live("mouseout", function() {
						$(this).removeClass("bh");
						$(this).find(".opBox").hide();

					});

					$(".rmBox").live("click", function() {
						$(this).parent().remove();
						$(".lentity .uriBox .item:last").addClass("last");

						enableSave();
					});

					$(".lentity .add_op")
							.live(
									"click",
									function() {
										if ($(".editing").length > 0) {
											if (displayLang == "en")
												$.messager
														.alert(
																'Error',
																'Please confirm you operation!',
																'error');
											else if (displayLang == "zh")
												$.messager.alert('错误',
														'请确认你的操作!', 'error');
											return;
										}
										$(".last").removeClass("last");
										var item = $("<div class='item last editing'></div>");
										var input = $("<input type='text' class='new_item'/>");
										item.append(input);
										var rmBox = $("<a class='rmBox' href='javascript:void(0)'><span class='rm_box'></span></a>");
										if(displayLang=="en"){
											rmBox.attr("title",'Remove this URI');
										}else if(displayLang=="zh"){
											rmBox.attr("title",'删除这个URI');
										}
										item.append(rmBox);
										rmBox.show();
										var okBox = $("<a class='okBox' href='javascript:void(0)'><span class='ok_box'></span></a>");
										if(displayLang=="en")
											okBox.attr("title","Done");
										else if(displayLang=="zh")
											okBox.attr("title","完成");
										item.append(okBox);
										$(".lentity .uriBox .content").append(
												item);
									});

					$(".okBox")
							.live(
									"click",
									function() {
										var link = $(this).parent().find(
												"input").val().trim();
										if (link.indexOf("http://") != 0) {
											if (displayLang == "en")
												$.messager
														.alert(
																'Error',
																'Your input is not valid! It must start with "http://"',
																'error');
											else if (displayLang == "zh")
												$.messager.alert('错误',
														'输入非法!必须以http://开头',
														'error');
											return;
										}
										var container = $(this).parent();
										container.html("");
										var linkBox = $(
												"<a target='_blank' class='link'></a>")
												.html(link);
										container.append(linkBox);
										var rmBox = $("<a class='opBox rmBox' href='javascript:void(0)'><span class='rm_box'></span></a>");
										if(displayLang=="en"){
											rmBox.attr("title",'Remove this URI');
										}else if(displayLang=="zh"){
											rmBox.attr("title",'删除这个URI');
										}
										container.append(rmBox);
										container.removeClass("editing");

										var res = checkMemberByURI(link);

										addMember(container, res.linkID,
												res.uri, res.label, res.entity);
									});
					/* end:uri memember operation */

					/* entity rename */
					$(".lentity .titlebar .renameBox input").live("click",
							function(event) {
								event.stopPropagation();
								rename(this);
							});
					/* end:entity rename */

					/* save entity */
					$(".save_button")
							.live(
									"click",
									function() {
										var ancestor = $(this).parent()
												.parent().parent().parent();
										var etype = ancestor.attr("etype");
										var eid = ancestor.attr("eid");
										if ($(".lentity .editing").length > 0) {
											if (displayLang == "en")
												$.messager
														.alert(
																'Error',
																'Please confirm your operation!',
																'error');
											else if (displayLang == "zh")
												$.messager.alert('错误',
														'请确认你的操作!', 'error');
											return;
										}
										if ($(".lentity .header .titlebar .name input").length > 0) {
											if (displayLang == "en")
												$.messager
														.alert(
																'Error',
																'Please confirm your operation!',
																'error');
											else if (displayLang == "zh")
												$.messager.alert('错误',
														'请确认你的操作!', 'error');
											return;
										}

										var entityName = $(
												".lentity .header .titlebar .name")
												.html();

										if (checkEntityName(entityName, etype,
												eid)) {
											if (displayLang == "en")
												$.messager
														.alert(
																'Error',
																'The dataset name has existed in current group!',
																'error');
											else if (displayLang == "zh")
												$.messager.alert('错误',
														'当前编辑组中已存在同名数据集!', 'error');
											return;
										}
										var array = new Array();
										ancestor.find(".item").each(
												function() {
													var uid = $(this).find("a.link")
															.attr("uid");
													array.push(uid);
												});
//										var tarray = new Array();
//										ancestor
//												.find(".typeBox .content span")
//												.each(
//														function() {
//															var cid = $(this)
//																	.attr("cid");
//															tarray.push(cid);
//														});
										var result = saveEntity(entityName,
												etype, eid, array);
										var resType = result.etype;
										//===for error
										if(resType=="failed"){
											if(lang == "en"){
												alert("Failed,please try latter!");
											}else if(lang=="zh"){
												alert("保存失败，请稍后再试");
											}
										}
										//=====
										var resID = result.eid;
										ancestor.attr("etype", resType);
										ancestor.attr("eid", resID);
										if (eid < 0) {
											var li = $("<li></li>");
											$(".leftnav").prepend(li);
											var a = $(
													"<a class='nav-item' href='javascript:void(0)'></a>")
													.html(entityName);
											a.attr("etype", resType);
											a.attr("eid", resID);
											a.attr("gid", uGid);
											a.attr('gname', uGname);
											li.append(a);

											$(".leftnav a.nav-select").each(
													function() {
														$(this).removeClass(
																"nav-select");
														$(this).parent().find(
																".nav-arrow")
																.remove();
													});
											$(a).addClass("nav-select");

											var span = $("<span class='nav-arrow'></span>");
											$(a).parent().append(span);
										} else {
											$(".leftnav").find(
													"a[eid=" + resID + "]")
													.html(entityName);
										}

										/* save button */
										disableSave();

									});
					/* end:save entity */

					/* recommend */
					$(".moreLess a.more").live("click", function() {
						$(this).parent().parent().find(".hidden").show();
						$(this).parent().find("a").removeClass("more");
						$(this).parent().find("a").addClass("less");
						if(lang == 'en'){
							$(this).parent().find("a").html("Less-");
						}else if(lang == 'zh'){
							$(this).parent().find("a").html("收起-");
						}
						
					});

					$(".moreLess a.less").live("click", function() {
						$(this).parent().parent().find(".hidden").hide();
						$(this).parent().find("a").removeClass("less");
						$(this).parent().find("a").addClass("more");
						if(lang == 'en'){
							$(this).parent().find("a").html("More+");
						}else if(lang == 'zh'){
							$(this).parent().find("a").html("更多-");
						}
					});

					$(".add-uri-op").live("click", function() {
						if ($(".entity-list .lentity").length == 0)
							return;
						
						var box =  $(this).parent().find("h3 a");
						var link = box.attr("title");
						
						var container = $("<div class='item'></div>");
						$(".lentity .uriBox .content .item:last").removeClass("last");
						$(".lentity .uriBox .content").append(container);
						
						var linkBox = $(
								"<a target='_blank' class='link'></a>")
								.html(link);
						container.append(linkBox);
						var rmBox = $("<a class='opBox rmBox' href='javascript:void(0)'><span class='rm_box'></span></a>");
						if(displayLang=="en"){
							rmBox.attr("title",'Remove this URI');
						}else if(displayLang=="zh"){
							rmBox.attr("title",'删除这个URI');
						}
						
						container.append(rmBox);

						var res = checkMemberByURI(link);

						addMember(container, res.linkID,
								res.uri, res.label);//, res.entity);
						$(".lentity .uriBox .content .item:last").addClass("last");

					});
					/* end:recommend */
				});

function addMember(container, linkID, uri, label){//, entityArray) {///=========???
	var ancestor = $(".lentity");
	var ances_etype = ancestor.attr("etype");
	var ances_eid = ancestor.attr("eid");
	if (ancestor.find(".uriBox a[uid=" + linkID + "]").length > 0) {
		var hints = $("<div class='hints'></div>");
		if(displayLang=="en"){
			hints.html('This URI has already been a mememer.');
		}else if(displayLang=="zh")
			hints.html('这个URI已经是成员.');
		hints.appendTo($(".entity-list")).show("fold");
		setTimeout(function() {
			$(".hints").fadeIn(1000);
			$(".hints").remove();
		}, 1000);
		container.remove();
		ancestor.find(".uriBox .item:last").addClass("last");
	}
	var linkBox = container.find("a.link");
	linkBox.attr("uid", linkID);
	var href = "/sview/dataset/u" + linkID;//"entity/o" + linkID;
	linkBox.attr("href", href);
	linkBox.attr("title", uri);
	linkBox.html(label);

//	var etype = null, eid = null, ename = null;
//	for (i = 0; i < entityArray.length; i++) {
//		var item = entityArray[i];
//		if (item.etype != ances_etype || item.eid != ances_eid) {
//			etype = item.etype;
//			eid = item.eid;
//			ename = item.name;
//			break;
//		}
//		;
//	}
//	if (etype != null) {
//		var dialog = $(".warning");
//		dialog.find(".ename").html(ename);
//		var dialog_title;
//		if (displayLang == "en") {
//			dialog_title = "Warning";
//		} else if (displayLang == "zh") {
//			dialog_title = "警告";
//		}
//		dialog.show();
//		dialog.dialog({
//			title : dialog_title,
//			iconCls : 'icon-warn',
//			modal : true,
//			width : 500,
//			buttons : [
//					{
//						text : 'Ok',
//						iconCls : 'icon-save',
//						handler : function() {
//							dialog.dialog('close');
//							$(
//									".leftnav a[etype=" + etype + "][eid="
//											+ eid + "]").parent().remove();
//							mergeEntity(ancestor, etype, eid);
//							enableSave();
//						}
//					},
//					{
//						text : 'Cancel',
//						handler : function() {
//							dialog.dialog('close');
//							container.remove();
//							ancestor.find(".uriBox .item:last")
//									.addClass("last");
//						}
//					} ],
//			onClose : function() {
//				container.remove();
//				ancestor.find(".uriBox .item:last").addClass("last");
//			}
//
//		});
//	}

	enableSave();
}

function cancelEditName(element, value) {
	element.html(value);
	var span = $('<span class="renameBox"></span>');
	var input = $('<input type="image" src="/sview/icons/page_white_edit.png" >');
	if (displayLang == "en")
		input.attr('title', 'Rename');
	else if (displayLang == "zh")
		input.attr('title', '重命名');
	span.append(input);
	element.parent().find(".name").after(span);
	element.parent().find(".saveBox").remove();
	element.parent().find(".cancelBox").remove();
}

function checkEntityName(name, etype, eid) {
	var result = false;
	var data = {};
//	data["groupID"] = Gid;
	data["name"] = name;
	data["etype"] = etype;
	data["eid"] = eid;

	data = JSON.stringify(data);
	data = "checkname=" + encodeURIComponent(data);

	$.ajax({
		url : "/sview/dataset",//"entity",//====
		async : false,
		data : data,
		dataType : "text",
		type : "post",
		success : function(data) {
			if (data == "conflict")
				result = true;
		}
	});
	return result;
}

function checkMemberByURI(link) {
	var result;
	var data = {};
//	data["groupID"] = Gid;
	data["link"] = link;
	data["lang"] = displayLang;

	data = JSON.stringify(data);
	data = "checkmemURI=" + encodeURIComponent(data);
	$.ajax({
		url : "/sview/dataset",//"entity",//=====?????
		async : false,
		data : data,
		dataType : "json",
		type : "post",
		success : function(data) {
			result = data;
		}
	});
	return result;
}


function createEditDatasetBox(name, ds_type, ds_id, gname, datasetType, datasetId) {
	$(".leftPanel").html("");
	var outerBox = $(".leftPanel");
	var list = $('<div class="entity-list"></div>');
	outerBox.append(list);
	var lentity = $('<div class="lentity"></div>');
	list.append(lentity);
	var header = $('<div class="header"></div>');
	lentity.append(header);
	lentity.attr("etype", ds_type);
	lentity.attr("eid", ds_id);
	var titlebar = $('<div class="titlebar"></div>');
	header.append(titlebar);
	var ename = $('<span class="name"></span>');
	ename.html(name);
	titlebar.append(ename);
	var renameBox = $('<span class="renameBox"><input type="image" src="/sview/icons/page_white_edit.png" ></span>');
	if(displayLang=="en"){
		renameBox.find("input").attr("title","Rename");
	}
	else if(displayLang=="zh"){
		renameBox.find("input").attr("title","重命名");
	}
	titlebar.append(renameBox);
	var vbody = $('<div class="ebody"></div>');
	lentity.append(vbody);
	var infoToolbar = createInfoToolBar(gname);
	vbody.append(infoToolbar);

//	var typeBox = createTypeBox(ds_type, ds_id);
//	vbody.append(typeBox);
//	getTypes(typeBox.find(".content"), ds_type, ds_id, datasetType, datasetId);//========

	var uriBox = createURIBox(ds_type, ds_id);
	vbody.append(uriBox);
	getMembers(uriBox.find(".content"), ds_type, ds_id, "l", datasetType, datasetId);

	/* save button */
	if (ds_id > 0) {
		disableSave();
	}

}

function createRecommendEntityBox(uriID, uri, label, typeArray, origin, sameas,
		social) {
	var outerBox = $(".recommendSection .content .entityList");
	var rentity = $('<li class="rentity"></li>');
	outerBox.append(rentity);
	var addOp = $("<span class='add-uri-op'></span>");
	var addOplabel = $("<span></span>");
	addOp.append(addOplabel);
	var displayLang = $("#display-lang").val();//=============
	if (displayLang == "en") {
		addOplabel.html("Add it");
	} else if (displayLang == "zh") {
		addOplabel.html("加入");
	}
	rentity.append(addOp);
	var titleDiv = $("<div class='title'></div>");
	rentity.append(titleDiv);
	var h3 = $("<h3></h3>");
	titleDiv.append(h3);
	var titleLink = $("<a target='_blank'></a>").html(label);
	var href = "/sview/dataset/u" + uriID;
	titleLink.attr("href", href);
	titleLink.attr("title", uri);
	h3.append(titleLink);

//	var typeBox = $("<div class='typeBox'></div>");
//	rentity.append(typeBox);
//	for (i = 0; i < typeArray.length; i++) {
//		var typeItem = typeArray[i];
//		var span = $("<span></span>");
//		span.html(typeItem.label);
//		span.attr("cid", typeItem.id);
//		typeBox.append(span);
//		if (i != typeArray.length - 1) {
//			var label = $("<label>,</label>");
//			typeBox.append(label);
//		}
//	}

	var contentDiv = $("<div class='content'></div>");
	rentity.append(contentDiv);
	var proofList = $("<div class='proofList'></div>");
	contentDiv.append(proofList);
	var count = 0;
	var box = proofList;
	for (i = 0; i < origin.length; i++) {
		var oitem = origin[i];
		var oid = oitem.id;
		var ouri = oitem.uri;
		var olabel = oitem.label;

		var sameasItem = sameas[i];
		var socialItem = social[i];

		if (sameasItem == true) {
			count++;
			if (count == initCount + 1) {
				var hidden = $("<div class='hidden'></div>");
				proofList.append(hidden);
				box = hidden;
			}
			var pitem = $("<div class='pitem'></div>");
			box.append(pitem);
			var div1 = $("<div></div>");
			var spanSameas = $("<span class='pc'></span>");
			if (displayLang == "en") {
				spanSameas.html("Cooccurrence of");
			} else if (displayLang == "zh") {
				spanSameas.html("同现uri");
			}
			div1.append(spanSameas);
			var spanURI = $("<span></span>").html(olabel);
			spanURI.attr("title", ouri);
			div1.append(spanURI);

			pitem.append(div1);

//			var div2;
//			if (displayLang == "en") {
//				div2 = $("<div><span class='pc'>Identitfied by<span class='pem'>Falcons Coreference Service</span></span></div>");
//			} else if (displayLang == "zh") {
//				div2 = $("<div><span class='pc'>经<span class='pem'>Falcons同指服务</span><span class='pr'>认证</span></span></div>");
//			}
//			pitem.append(div2);
		}

		if (socialItem.numerator > 0) {
			count++;
			if (count == initCount + 1) {
				var hidden = $("<div class='hidden'></div>");
				proofList.append(hidden);
				box = hidden;
			}

			var pitem = $("<div class='pitem'></div>");
			box.append(pitem);

			var div1 = $("<div></div>");
			pitem.append(div1);
			var spanGroup = $("<span class='pc'></span>");
			div1.append(spanGroup);
			var label1 = $("<label></label>");
			if (displayLang == "en") {
				label1.html('Co-occur ');
			} else if (displayLang == "zh") {
				label1.html = ("同现");
			}
//			alert(displayLang+label1.html());
			spanGroup.append(label1);

			var scoreSpan = $("<span class='pem'></span>");
			spanGroup.append(scoreSpan);
			var labelNumerator = $("<label></label>")
					.html(socialItem.numerator);
			scoreSpan.append(labelNumerator);
			var labelSlash;
			if(displayLang == "en"){
				labelSlash = $("<label> times in </label>");
			} else if(displayLang == "zh"){
				labelSlash = $("<label> 次在  </label>个");
			}
			
			scoreSpan.append(labelSlash);
			var labelDenominator = $("<label></label>").html(
					socialItem.denominator);
			scoreSpan.append(labelDenominator);

			var label2 = $("<label></label>");
			if (displayLang == "en") {
				label2.html("custom datasets");
			} else if (displayLang == "zh") {
				label2.html("自定义数据集中");
			}
			spanGroup.append(label2);

//			var div2 = $("<div></div>");
//			pitem.append(div2);
//			var spanRelated = $("<span class='pc'></span>");
//			if (displayLang == "en") {
//				spanRelated.html("Related to");
//			} else if (displayLang == "zh") {
//				spanRelated.html("有关");
//			}
//			div2.append(spanRelated);
//			var spanURI = $("<span></span>").html(olabel);
//			spanURI.attr("title", ouri);
//			div2.append(spanURI);
		}
	}

	if (count > 4) {
		var showTitle;
		if(lang == 'en'){
			showTitle = "More+";
		}else if(lang == 'zh'){
			showTitle = "更多+";
		}
		var moreDiv = $("<div class='moreLess'><a class='more' href='javascript:void(0)'></a>"+showTitle+"</div>");
		proofList.append(moreDiv);
	}
}

function createInfoToolBar(gname) {
	var toolbar = $('<div class="tool_bar_info"></div>');
	var ginfo = $('<div class="group_info"></div>');
	toolbar.append(ginfo);
	var label = $("<label></label>");
	if(displayLang=="en")
		label.html("By group");
	else if(displayLang=="zh")
		label.html("属于组");
	ginfo.append(label);
	var span = $('<span class="gname"></span>').html(gname);
	ginfo.append(span);
	return toolbar;
}

function createRightURIBox(etype, eid) {
	var box = $('<div class="uriBox"></div>');
	var toolbar = $('<div class="tool_bar"></div>');
	box.append(toolbar);
	var label = $("<label class='title'>URIs</label>");
	toolbar.append(label);
	var tools = $("<div class='tools'></div>");
	toolbar.append(tools);
	var alink = $('<a href="javascript:void(0)" class="easyui-linkbutton add_op"'
			+ '	iconCls="icon-addAll" plain="true" ></a>');
	if (displayLang == "en") {
		alink.attr("title",
				"Add all the URIs of this dataset to the dataset of my group");
	} else if (displayLang == "zh") {
		alink.attr("title", "向我的编辑组的数据集中加入所有的URI");
	}
	tools.append(alink);
	alink.linkbutton();
	var content = $('<div class="content"></div>');
	box.append(content);

	return box;
}

function createURIBox(etype, eid) {
	var box = $('<div class="uriBox"></div>');
	var toolbar = $('<div class="tool_bar"></div>');
	box.append(toolbar);
	var label = $("<label class='title'>URIs</label>");
	toolbar.append(label);
	var tools = $("<div class='tools'></div>");
	toolbar.append(tools);
	var alink = $('<a href="javascript:void(0)" class="easyui-linkbutton add_op"'
			+ '	iconCls="icon-add" plain="true" ></a>');
	if (displayLang == "en") {
		alink.attr("title", "Add a URI");
	} else if (displayLang == "zh") {
		alink.attr("title", "添加URI");
	}
	tools.append(alink);
	alink.linkbutton();
	var content = $('<div class="content"></div>');
	box.append(content);

	var dialog;
	if(displayLang=="en")
		dialog= $("<div class='dialog-button toolbar'><a class='easyui-linkbutton save_button' iconCls='icon-save'"
			+ "	href='javascript:void(0)'>Save</a><a class='easyui-linkbutton close_button' href='javascript:void(0)'>Close</a></div>");
	else if(displayLang=="zh"){
		dialog= $("<div class='dialog-button toolbar'><a class='easyui-linkbutton save_button' iconCls='icon-save'"
				+ "	href='javascript:void(0)'>保存</a><a class='easyui-linkbutton close_button' href='javascript:void(0)'>关闭</a></div>");
	}
	box.append(dialog);
	dialog.find("a").linkbutton();
	return box;
}

//function createTypeBox(etype, eid) {
//	var box = $('<div class="typeBox"></div>');
//	var toolbar = $('<div class="tool_bar"></div>');
//	box.append(toolbar);
//	var label = $("<label class='title'></label>");
//	if(displayLang=="en")
//		label.html("Type");
//	else if(displayLang=="zh")
//		label.html("类型");
//	toolbar.append(label);
//	var content = $('<div class="content"></div>');
//	box.append(content);
//	return box;
//}

//changed
function getMyGroupEntity(groupID, etype, eid, lang) {
	var data = {};
	data["etype"] = etype;
	data["lang"] = displayLang;
	data["eid"] = eid;
//	data["groupID"] = groupID;

	data = JSON.stringify(data);
	data = "mgdataset=" + encodeURIComponent(data);

	$.ajax({
		url : "/sview/dataset",
		async : false,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			$(".leftnav").html("");
			for (i = 0; i < result.length; i++) {
				var item = result[i];
				var name = item.name;
				var etype = item.ds_type;//==
				var eid = item.datasetId;//===
				var gid = item.gid;
				var gname = item.gname;
				var li = $("<li></li>");
				$(".leftnav").append(li);
				var a = $("<a class='nav-item' href='javascript:void(0)'></a>")
						.html(name);
				a.attr("etype", etype);
				a.attr("eid", eid);
				a.attr("gid", gid);
				a.attr('gname', gname);
				li.append(a);
			}
		}
	});
}

function getMembers(box, etype, eid, pos, entityType, entityID) {
	var data = {};
	data["etype"] = etype;
	data["lang"] = displayLang;
	data["eid"] = eid;
//	data["groupID"] = Gid;
	if (entityType) {
		data["s_etype"] = entityType;
		data["s_eid"] = entityID;
	}
	data = JSON.stringify(data);
	data = "gmem=" + encodeURIComponent(data);
	$.ajax({
		url : "/sview/dataset",//"entity",
		async : false,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			box.html("");
			for (i = 0; i < result.length; i++) {
				var item = result[i];
				var uriID = item.uriID;
				var uri = item.uri;
				var label = item.label;
				var div = $("<div class='item'></div>");
				box.append(div);
				var alink = $('<a  target="_blank" class="link"></a>').html(label);
				var href = "/sview/dataset/u" + uriID;
				alink.attr("href", href);
				alink.attr("uid", uriID);
				alink.attr("title", uri);
				div.append(alink);
				var op;
				if (pos == "l") {
					op = $("<a class='opBox rmBox' href='javascript:void(0)'>"
							+ "<span class='rm_box'></span></a>");
					if (displayLang == "en") {
						op.attr("title", "Remove this URI");
					} else if (displayLang == "zh") {
						op.attr("title", "删除这个URI");
					}
				} else if (pos == "r") {
					op = $("<a class='opBox addBox' href='javascript:void(0)'>"
							+ "<span class='add_box'></span></a>");
					if (displayLang == "en") {
						op.attr("title",
								"Add this URI to the entity of my group");
					} else if (displayLang == "zh") {
						op.attr("title", "向我的组的实体中加入这个URI");
					}
				}
				div.append(op);
			}
			box.find(".item:last").addClass("last");
		}
	});
}

function getRecommendEntity(groupID, etype, eid, lang) {
	var data = {};
	data["etype"] = etype;
	data["lang"] = displayLang;
	data["eid"] = eid;
//	data["groupID"] = groupID;

	data = JSON.stringify(data);
	data = "recommendEntity=" + encodeURIComponent(data);

	$.ajax({
		url : "/sview/dataset",//"entity",
		async : false,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			for (k = 0; k < result.length; k++) {
				var item = result[k];
				var uriID = item.uriID;
				var uri = item.uri;
				var label = item.label;
				var typeArray = item.type;
				var origin = item.origin;
				var sameas = item.sameas;
				var social = item.social;
				createRecommendEntityBox(uriID, uri, label, typeArray, origin,
						sameas, social);
			}
		}
	});
}

//function getTypes(box, etype, eid, entityType, entityID) {
//	var data = {};
//	data["etype"] = etype;
//	data["lang"] = displayLang;
//	data["eid"] = eid;
//	data["groupID"] = Gid;
//	if (entityType) {
//		data["s_etype"] = entityType;
//		data["s_eid"] = entityID;
//	}
//	data = JSON.stringify(data);
//	data = "gtype=" + encodeURIComponent(data);
//	$.ajax({
//		url : "entity",
//		async : false,
//		data : data,
//		dataType : "json",
//		type : "post",
//		success : function(result) {
//			box.html("");
//			for (i = 0; i < result.length; i++) {
//				var item = result[i];
//				var classID = item.classID;
//				var label = item.label;
//				if (box.find("span[cid=" + classID + "]").length > 0)
//					continue;
//				var span = $("<span></span>").html(label);
//				span.attr("cid", classID);
//				box.append(span);
//				if (i != result.length - 1) {
//					var l = $("<label>,<label>");
//					box.append(l);
//				}
//			}
//		}
//	});
//}

function init() {
	getMyGroupEntity(Gid, Etype, Eid, displayLang);
	getRecommendEntity(Gid, Etype, Eid, displayLang);
}

//function mergeEntity(ancestor, etype, eid) {
//	var data = {};
//	data["etype"] = String(etype);
//	data["eid"] = String(eid);
//	data["lang"] = displayLang;
//
//	data = JSON.stringify(data);
//	data = "mergeEntity=" + encodeURIComponent(data);
//	if (eid < 0)
//		return;
//	$.ajax({
//		url : "entity",
//		async : false,
//		data : data,
//		dataType : "json",
//		type : "post",
//		success : function(result) {
//			ancestor.find(".item:last").removeClass("last");
//			var box = ancestor.find(".uriBox .content");
//			for (i = 0; i < result.length; i++) {
//				var item = result[i];
//				var id = item.id;
//				var uri = item.uri;
//				var label = item.label;
//				if (box.find("a[uid=" + id + "]").length > 0)
//					continue;
//				var div = $("<div class='item'></div>");
//				box.append(div);
//				var alink = $('<a target="_blank" class="link"></a>').html(label);
//				var href='entity/o'+id;
//				alink.attr("href",href);
//				alink.attr("title", uri);
//				alink.attr("uid", id);
//				div.append(alink);
//				var op = $("<a class='opBox rmBox' href='javascript:void(0)'>"
//						+ "<span class='rm_box'></span></a>");
//				if (displayLang == "en") {
//					op.attr("title", "Remove this URI");
//				} else if (displayLang == "zh") {
//					op.attr("title", "删除这个URI");
//				}
//				div.append(op);
//			}
//			ancestor.find(".item:last").addClass("last");
//		}
//	});
//}

function rename(e) {
	// see if the wrapper is origin
	var editPanel = $(e).parent().parent().parent().parent();
	var etype = editPanel.attr("etype");
	var eid = editPanel.attr("eid");
	var element = $(e).parent().parent().find(".name");
	var textBox = $('<input type="text" class="test"></input>');
	var value = element.text();
	textBox.attr('value', value);
	element.html('');
	element.append(textBox);
	textBox.focus();
	textBox.select();
	textBox.click(function(event) {
		event.stopPropagation();
	});
	element.parent().find(".renameBox").remove();
	var _save = $("<a class='saveBox'></a>");
	var title_save = null;
	if (displayLang == "en")
		title_save = "OK";
	else if (displayLang == "zh")
		title_save = "确定";
	_save.attr("title", title_save);
	var _cancel = $("<a class='cancelBox'></a>");
	var title_cancel = null;
	if (displayLang == "en")
		title_cancel = "Cancel";
	else if (displayLang == "zh")
		title_cancel = "取消";
	_cancel.attr("title", title_cancel);
	element.after(_save);
	_save.after(_cancel);
	$(_save).click(function(event) {
		event.stopPropagation();
		saveEditName(element, textBox.val(), value, etype, eid);
	});
	$(_cancel).click(function(event) {
		editPanel.find(".error-info").remove();
		event.stopPropagation();
		cancelEditName(element, value);
	});
}

function saveEditName(element, value, valueOld, etype, eid) {
	if (value.trim() == "") {
		if (displayLang == "en")
			$.messager.alert('Error', 'The name can not be empty!', 'error');
		else if (displayLang == "zh")
			$.messager.alert('Error', '名字不能为空!', 'error');
		element.html(valueOld);
		var span = $('<span class="renameBox"></span>');
		var input = $('<input type="image" src="/sview/icons/page_white_edit.png" >');
		if (displayLang == "en")
			input.attr('title', 'Rename');
		else if (displayLang == "zh")
			input.attr('title', '重命名');
		span.append(input);
		element.parent().find(".name").after(span);
		element.parent().find(".saveBox").remove();
		element.parent().find(".cancelBox").remove();
	} else {
		element.html(value);
		var span = $('<span class="renameBox"></span>');
		var input = $('<input type="image" src="/sview/icons/page_white_edit.png" >');
		if (displayLang == "en")
			input.attr('title', 'Rename');
		else if (displayLang == "zh")
			input.attr('title', '重命名');
		span.append(input);
		element.parent().find(".name").after(span);
		element.parent().find(".saveBox").remove();
		element.parent().find(".cancelBox").remove();
	}

	/* save button */
	enableSave();
}

function saveEntity(name, etype, eid, array) {
	var result;
	var data = {};
//	data["groupID"] = Gid;
	data["name"] = name;
	data["etype"] = etype;
	data["eid"] = eid;
	data["member"] = array;
	data["lang"] = displayLang;
	data["user"] = getUser();
	data = JSON.stringify(data);
	data = "svdataset=" + encodeURIComponent(data);
	$.ajax({
		url : "/sview/dataset",//"entity",
		async : false,
		data : data,
		dataType : "json",
		type : "post",
		success : function(data) {
			result = data;
		}
	});
	return result;
}

function unselect(li) {
	li.find("span").remove();
	li.find("a").removeClass("nav-select");
}

function enableSave() {
	$(".lentity a.save_button").removeClass("disabled");
}

function disableSave() {
	$(".lentity a.save_button").addClass("disabled");
}
